iT邦幫忙

2024 iThome 鐵人賽

DAY 7
0
Kubernetes

從零到一: 使用Spring Boot、Kubernetes 和 Istio實現微服務架構系列 第 7

Day 7 使用Spring Boot、Kubernetes 和 Istio實現微服務架構 - 建立Class跟Repository

  • 分享至 

  • xImage
  •  

1-5 Restful API

首先我們要建立一個Class裡面放我們要的參數

package com.example.glucon.Student;

import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDate;

@Entity
@Table
public class Student {
   @Id
   @SequenceGenerator(
           name = "student_sequence",
           sequenceName = "student_sequence",
           allocationSize = 1

   )
   @GeneratedValue(
           strategy = GenerationType.SEQUENCE,
           generator = "student_sequence"
   )
   private Long id;
   private String name;
   private String email;
   private LocalDate dob;
   private Integer age;

   public Student() {
   }

   public Student(Long id,
                  String name,
                  String email,
                  LocalDate dob,
                  Integer age) {
       this.id = id;
       this.name = name;
       this.email = email;
       this.dob = dob;
       this.age = age;
   }

   public Student(String name,
                  String email,
                  LocalDate dob,
                  Integer age) {
       this.name = name;
       this.email = email;
       this.dob = dob;
       this.age = age;
   }

   public Long getId() {
       return id;
   }

   public void setId(Long id) {
       this.id = id;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getEmail() {
       return email;
   }

   public void setEmail(String email) {
       this.email = email;
   }

   public LocalDate getDob() {
       return dob;
   }

   public void setDob(LocalDate dob) {
       this.dob = dob;
   }

   public Integer getAge() {
       return age;
   }

   public void setAge(Integer age) {
       this.age = age;
   }

   @Override
   public String toString() {
       return "Student{" +
               "id=" + id +
               ", name='" + name + '\'' +
               ", email='" + email + '\'' +
               ", dob=" + dob +
               ", age=" + age +
               '}';
   }
}

接著是 Repository

package com.example.glucon.Student;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface StudentRepository
        extends JpaRepository<Student,Long> {
    @Query("SELECT s FROM Student s where s.email = ?1")
    Optional<Student> findStudentByEmail(String email);
}

明天就會開始實作Service的部分了!


上一篇
Day 6 使用Spring Boot、Kubernetes 和 Istio實現微服務架構 - Hello World
下一篇
Day 8 使用Spring Boot、Kubernetes 和 Istio實現微服務架構 - 透過pgadmin4操作資料庫
系列文
從零到一: 使用Spring Boot、Kubernetes 和 Istio實現微服務架構25
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言